import { HealthCheckWithScreenTask } from "./health.check.class";
import { PanelGroupKey, SendboxKey, V3proSubjectMessage } from "../model/v3pro.model";
import { V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proFileHelper } from "../helper/v3pro.file.helper";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { appConfig } from "../../../configs/env.config.load";
import { Subject } from "rxjs";
import {
  SRJSONFile,
  SRPanelGroupFile,
  SRScanBoardFile,
  SRSendBoxFile,
} from "../model/v3pro.file.model";
import { V3proCheckScanboardTemperatureTask } from "./v3pro.check.scanboard.temperature.task";
import { V3proCheckPanelTemperatureTask } from "./v3pro.check.panel.temperature.task";
import { V3proCheckSendboxTemperatureTask } from "./v3pro.check.sendbox.temperature.task";
import { V3proScreenImageHelper } from "../helper/v3pro.screen.image.helper";

/**
 * 温度异常
 */
export class V3proCheckTemperatureTask extends HealthCheckWithScreenTask<SRJSONFile> {
  async checkResult(
    keyParams: SRJSONFile,
    screenId: string,
    allStatus: V3proScreenAllStatus,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<V3proSubjectMessage> {
    const message = new V3proSubjectMessage();
    if (keyParams.getExistCount() === 0) return message;
    message.setErrStatus();
    keyParams["send-boxes"]?.forEach((sendBox: SendboxKey) => {
      screenLayoutHelper.setSendBoxErrById(sendBox.id, 2);
    });
    keyParams["scan-boards"]?.forEach((scanBoard) => {
      screenLayoutHelper.setScanBoardErrById(scanBoard.id, 0, 0);
    });
    keyParams["panel-groups"]?.forEach((panelGroup: PanelGroupKey) => {
      panelGroup.panels.forEach((panel) => {
        screenLayoutHelper.setPanelErrById(panelGroup.id, panel.index, 0);
      });
    });
    message.files = [
      await V3proFileHelper.srJson2File(keyParams),
      await new V3proScreenImageHelper(screenLayoutHelper, ["scanBoard", "panel"])
        .draw()
        .export2AlertMessageFile(),
    ];
    return message;
  }

  async genKeyParams(
    allStatus: V3proScreenAllStatus,
    screen: V3proScreen,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<SRJSONFile> {
    const tasks = [
      new V3proCheckSendboxTemperatureTask(allStatus, screenLayoutHelper),
      new V3proCheckScanboardTemperatureTask(allStatus, screenLayoutHelper),
      new V3proCheckPanelTemperatureTask(allStatus, screenLayoutHelper),
    ];
    return new Promise((resolve, reject) => {
      try {
        const subject = new Subject<V3proSubjectMessage>();
        const messages = new SRJSONFile();
        let timeout = setTimeout(() => {
          subject.unsubscribe();
          reject("timeout");
        }, 10_000);
        subject.subscribe((message) => {
          if (message.alertType === V3proAlertMessageErrTypeEnum.SEND_BOX_TEMP) {
            SRSendBoxFile.buildToSRJSONFile(message.keyParams, messages);
          } else if (message.alertType === V3proAlertMessageErrTypeEnum.SCAN_BOARD_TEMP) {
            SRScanBoardFile.buildToSRJSONFile(message.keyParams, messages);
          } else if (message.alertType === V3proAlertMessageErrTypeEnum.PANEL_TEMP) {
            SRPanelGroupFile.buildToSRJSONFile(message.keyParams, messages);
          }
          if (messages.getCount() >= 3) {
            clearTimeout(timeout);
            resolve(messages);
          }
        });
        tasks.forEach((task) => task.setObservable(subject).start());
      } catch (e) {
        reject(e);
      }
    });
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return `notify/alert/ssads/device-sensor:temperature-over-limit/${screenId}`;
    // return "发送盒温度异常";
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum {
    return V3proAlertMessageErrTypeEnum.SEND_BOX_TEMP;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return message.isErrStatus();
  }

  isReportEnabled(keyParams: any, screenId: string): boolean {
    return appConfig.v3pro.errEnable.temperature;
  }
}
